<?
require_once('functions.php');

function GetOrders()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM 2012WSP_Orders');
}
function BlankOrder()
{
        return array('Order_id'=>null,'Cost'=>null,'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function GetOrder($id)
{
        $conn = GetConnection();
        $sql = "SELECT * FROM 2012WSP_Orders WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
		
}
function SaveOrder(&$row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        if(empty($row['id']))
        {
                $sql =  "Insert 2012WSP_Orders (Cost) "
                        .       " Values ('$row2[Cost]',  NOW() ) ";
        }else{
                $sql =  "UPDATE 2012WSP_Orders "
                        .       "Set Cost='$row2[Cost]'"
                        .       "WHERE id=$row2[id] ";
        }
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        if(empty($row['id']) && empty($error))
        {
                $row['id'] = $conn->insert_id;
        }
        $conn->close();
        
        return $error == '' ? true : array('SQL Error' => $error);
}

function NewOrder($row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        $sql =  "INSERT INTO 2012WSP_Orders	 (Order_id, Cost) VALUES('$row2[Order_id]', '$row2[Cost]')";
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        $conn->close();
        
        return $error == '' ? true : array('SQL Error' => $error);
}

function DeleteOrder($row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        $sql =  "DELETE FROM 2012WSP_Orders WHERE id='$row2[id]'";
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        $conn->close();
        
        return $error == '' ? true : array('SQL Error' => $error);
}
function ValidateOrder($row)
{
        $errors = array();
        if(empty($row['Cost'])) $errors['Cost'] = 'New Price is required';
        /*if(empty($row['LastName'])) $errors['LastName'] = 'Last Name is required';
        if(!is_numeric($row['Order_id'])) $errors['Order_id'] = 'Order_id must be a number';
        if(empty($row['Order_id'])) $errors['Order_id'] = 'Order_id is required';*/
        
        return count($errors) == 0 ? true : $errors;
}
